//
//  NewsViewController.m
//  ShangAPP
//
//  Created by ---☆渴望☆--- on 16/2/22.
//  Copyright © 2016年 wangdao. All rights reserved.
//

#import "NewsViewController.h"
#import "MyTools.h"
#import <MJRefresh.h>
#import "NewsTableViewCell.h"
#import <UIImageView+WebCache.h>

@interface NewsViewController ()<UITableViewDataSource,
                                 UITableViewDelegate>
{
    UITableView      * _myTab;
    NSDictionary     * _dic;
    NSArray          * _myarr;
}
@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title = @"消息中心";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
    NSUserDefaults * user =[NSUserDefaults standardUserDefaults];
    [user setObject:@"xiao" forKey:@"xiaoxi"];
    [user synchronize];
    
//    [self createUI];
    [self requsetData];
    
}

-(void)requsetData
{
    NSString *url = @"http://www.xsy777.com/app/demo/newslist.html";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{@"user_token":[user objectForKey:@"token"],
                           @"page":@"1"};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [manager POST:url parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            _myarr = [[NSArray alloc]initWithArray:[responseObject objectForKey:@"data"]];
            
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        
        [self createUI];
        [_myTab reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}

- (void)createUI
{
    _myTab =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64)];
    _myTab.rowHeight = 50;
    _myTab.backgroundColor = UIColorFromRGB(0xefeff1);
    _myTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTab.showsVerticalScrollIndicator = NO;
//    _myTab.scrollEnabled = NO;
    [_myTab addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(shuaxin)];
    _myTab.delegate = self;
    _myTab.dataSource = self;
    [self.view addSubview:_myTab];
    
}

#pragma mark tableview代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _myarr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * str =@"kewang";
    NewsTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell =[[NewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
//    _dic =_myarr[indexPath.row][@"userinfo"]
    [cell.headimage sd_setImageWithURL:_myarr[indexPath.row][@"imgs"] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    cell.titleLab.text =[NSString stringWithFormat:@"%@",_myarr[indexPath.row][@"userinfo"][@"nickname"]];
    cell.contentLab.text =[NSString stringWithFormat:@"%@",_myarr[indexPath.row][@"content"]];
    return cell;
}


-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)shuaxin
{
    [self requsetData];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
